<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：tudougeaini
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open(',MB_realname','login',2);



switch ($mudi){
	case 'loadData':
		$menuFileID = 400;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		LoadData();
		break;

	case 'del':
		$menuFileID = 400;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		del();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 读取数据
function LoadData(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataMode	= OT::GetStr('dataMode');
	$dataModeStr= OT::GetStr('dataModeStr');
	$type		= OT::GetStr('type');
	$endDate	= OT::GetStr('endDate');

	if (! strtotime($endDate)){
		die('<span style="color:red;">删除日期（'. $endDate .'）不是有效时间格式。</span>');
	}

	$todayDate = TimeDate::Get('date');
	$dayDiff = TimeDate::Diff('d', $endDate, $todayDate);

	if ($type == 'domain'){
		$overDay = 45;
	}else{
		$overDay = 15;
	}

	if ($dayDiff < $overDay){
		if ($dayDiff < 0){
			$diffStr = '超过 '. abs($dayDiff) .' 天';
		}else{
			$diffStr = '只过期 '. $dayDiff .' 天';
		}
		die('<span style="color:red;">删除日期（'. $endDate .'）离今天'. $diffStr .'，必须过期超过'. $overDay .'天。</span>');
	}

	if ($type == 'domain'){
		$totalNum = $DB->GetOne('select count(DD_ID) from '. OT_dbPref .'domainData where DD_endDate<='. $DB->ForTime($endDate));
		$endNum = $DB->GetOne('select count(DD_ID) from '. OT_dbPref .'domainData where DD_state=1 and DD_endDate<='. $DB->ForTime($endDate));
		$state0Num = $DB->GetOne('select count(DD_ID) from '. OT_dbPref .'domainData where DD_state=0 and DD_endDate<='. $DB->ForTime($endDate));
		$state_Num = $DB->GetOne('select count(DD_ID) from '. OT_dbPref .'domainData where DD_state=-1 and DD_endDate<='. $DB->ForTime($endDate));

		echo('
		<input type="hidden" id="'. $type .'Total" name="'. $type .'Total" value="'. $totalNum .'" />
		<input type="hidden" id="'. $type .'Date2" name="'. $type .'Date2" value="'. $endDate .'" />
		
		过期 <span style="font-weight:bold;font-size:16px">'. $dayDiff .'</span> 天域名，共 <span style="color:blue;font-weight:bold;font-size:16px">'. $totalNum .'</span> 个
		&ensp;&ensp;&ensp;&ensp;<input type="submit" value=" 确定删除 " style="color:red;padding:5px;font-size:14px;">
		<br />
		其中 正常过期 <span style="color:red;font-weight:bold;font-size:16px">'. $endNum .'</span> 个，
		预注册成功 <span style="color:red;font-weight:bold;font-size:16px">'. $state0Num .'</span> 个，
		注册失败 <span style="color:red;font-weight:bold;font-size:16px">'. $state_Num .'</span> 个
		');

		$rNum = 0;
		$rexe = $DB->query('select DD_domain from '. OT_dbPref .'domainData where DD_endDate<='. $DB->ForTime($endDate));
		while ($row = $rexe->fetch()){
			$rNum ++;
			echo('<div style="line-height:1.2;">'. $rNum .'、'. $row['DD_domain'] .'</div>');
		}
		unset($rexe);

	}else{
		switch ($type){
			case 'host':	$tabPart = 'HU_';	$tabName = 'hostUsers';		$nameField = 'workSign';	break;
			case 'vpsApi':	$tabPart = 'VAU_';	$tabName = 'vpsApiUsers';	$nameField = 'ipStr';		break;
			case 'serv':	$tabPart = 'SU_';	$tabName = 'servUsers';		$nameField = 'ipStr';		break;
			default :		die($type .' 不是有效类型');	break;
		}
		$totalNum = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$endNum = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state>=10 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state9Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=9 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state8Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=8 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state6Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=6 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state5Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=5 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state3Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=3 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		$state0Num = $DB->GetOne('select count('. $tabPart .'ID) from '. OT_dbPref . $tabName .' where '. $tabPart .'state=0 and '. $tabPart .'endTime<='. $DB->ForTime($endDate));

		if ($endNum + $state9Num + $state8Num + $state6Num + $state5Num + $state3Num + $state0Num != $totalNum){
			$endNum = $state9Num = $state8Num = $state6Num = $state5Num = $state3Num = $state0Num = '-';
		}

		echo('
		<input type="hidden" id="'. $type .'Total" name="'. $type .'Total" value="'. $totalNum .'" />
		<input type="hidden" id="'. $type .'Date2" name="'. $type .'Date2" value="'. $endDate .'" />
		
		过期 <span style="font-weight:bold;font-size:16px">'. $dayDiff .'</span> 天机器，共 <span style="color:blue;font-weight:bold;font-size:16px">'. $totalNum .'</span> 台
		&ensp;&ensp;&ensp;&ensp;<input type="submit" value=" 确定删除 " style="color:red;padding:5px;font-size:14px;">
		<br />
		其中 正常过期 <span style="color:red;font-weight:bold;font-size:16px">'. $endNum .'</span> 台，
		已退款 <span style="color:red;font-weight:bold;font-size:16px">'. $state9Num .'</span> 台，
		退款中 <span style="color:red;font-weight:bold;font-size:16px">'. $state8Num .'</span> 台，
		开通失败 <span style="color:red;font-weight:bold;font-size:16px">'. $state6Num .'</span> 台，
		开通中 <span style="color:red;font-weight:bold;font-size:16px">'. $state5Num .'</span> 台，
		待开通 <span style="color:red;font-weight:bold;font-size:16px">'. $state3Num .'</span> 台，
		停止 <span style="color:red;font-weight:bold;font-size:16px">'. $state0Num .'</span> 台

		<table>
		');

		$rNum = 0;
		$rexe = $DB->query('select '. $tabPart .'proDataName,'. $tabPart .'vpsName,'. $tabPart . $nameField .','. $tabPart .'endTime,'. $tabPart .'state from '. OT_dbPref . $tabName .' where '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		while ($row = $rexe->fetch()){
			$rNum ++;
			echo('
			<tr>
				<td style="line-height:1.2;padding-right:8px;">'. $rNum .'、'. $row[$tabPart .'vpsName'] .'&ensp;</td>
				<td style="line-height:1.2;padding-right:8px;">'. $row[$tabPart .'proDataName'] .'&ensp;</td>
				<td style="line-height:1.2;padding-right:8px;">'. $row[$tabPart . $nameField] .'&ensp;</td>
				<td style="line-height:1.2;padding-right:8px;">'. IdcInfo::State($row[$tabPart .'state']) .'&ensp;</td>
				<td style="line-height:1.2;padding-right:8px;">到期：'. $row[$tabPart .'endTime'] .'</td>
			</tr>
			');
		}
		unset($rexe);

		echo('</table>');
	}
}



// 删除
function del(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataMode	= OT::PostStr('dataMode');
	$dataModeStr= OT::PostStr('dataModeStr');
	$type		= OT::GetStr('type');
	$endDate	= OT::PostStr($type .'Date');
	$delNum		= OT::PostInt($type .'Total');
	$delDate	= OT::PostStr($type .'Date2');

	if (! strtotime($endDate)){
		die('<span style="color:red;">删除日期（'. $endDate .'）不是有效时间格式。</span>');
	}
	if (! strtotime($delDate)){
		die('<span style="color:red;">再次确认删除日期（'. $delDate .'）不是有效时间格式。</span>');
	}
	if (strtotime($endDate) != strtotime($delDate)){
		die('<span style="color:red;">核准删除日期不匹配（删除日期：'. $endDate .'，核准日期：'. $delDate .'），请刷新页面再重新操作。</span>');
	}

	$todayDate = TimeDate::Get('date');
	$dayDiff = TimeDate::Diff('d', $endDate, $todayDate);
	if ($type == 'domain'){ $minDay = 45; }else{ $minDay = 8; }
	if ($dayDiff < $minDay){
		if ($dayDiff < 0){
			$diffStr = '超过 '. abs($dayDiff) .' 天';
		}else{
			$diffStr = '只过期 '. $dayDiff .' 天';
		}
		die('<span style="color:red;">删除日期（'. $endDate .'）离今天'. $diffStr .'，必须过期超过8天。</span>');
	}

	switch ($type){
		case 'domain':	$tabPart = 'DD_';	$tabName = 'domainData';	break;
		case 'host':	$tabPart = 'HU_';	$tabName = 'hostUsers';		break;
		case 'vpsApi':	$tabPart = 'VAU_';	$tabName = 'vpsApiUsers';	break;
		case 'serv':	$tabPart = 'SU_';	$tabName = 'servUsers';		break;
		default :		die($type .' 不是有效类型');	break;
	}

	if ($type == 'domain'){
		$judRes = $DB->query('delete from '. OT_dbPref .'domainData where DD_endDate<='. $DB->ForTime($endDate));
	}else{
		$chkexe = $DB->query('select '. $tabPart .'ID from '. OT_dbPref . $tabName .' where '. $tabPart .'endTime<='. $DB->ForTime($endDate));
		while ($row = $chkexe->fetch()){
			AdmIdc::AddDelUsers($type, $row[$tabPart .'ID']);
			$judRes = $DB->query('delete from '. OT_dbPref . $tabName .' where '. $tabPart .'ID='. $row[$tabPart .'ID']);
		}
		unset($chkexe);
		// $judRes = $DB->query('delete from '. OT_dbPref . $tabName .' where '. $tabPart .'endTime<='. $DB->ForTime($endDate));
	}
	if ($judRes){
		die('<span style="color:green;">删除成功</span>');
	}else{
		die('<span style="color:red;">删除失败</span>');
	}

}

?>